<?php
/**
 * ============================================================================
 * Author: Abing
 * Date: 17-3-21
 * 联系QQ:501443501
 * ============================================================================
 */
require_once __DIR__.'/errorCode.php';
class Article{
    /**
     * $_db数据库句柄
     * @var
     */
    private $_db;

     function __construct($_db)
    {
        $this->_db = $_db;
    }

    /**
     * //创建文章
     * @param $title
     * @param $content
     * @param $user_id
     * @throws Exception
     * @return array
     */
    public function create($title,$content,$user_id){
        if(empty($title)){
            throw new Exception('标题不能为空',ErrorCode::ARTICLE_TITLE_EMPTY);
        }
        if(empty($content)){
            throw new Exception('文章内容不能为空',ErrorCode::ACTICLE_CONTENT_EMPTY);
        }
        $sql = 'INSERT INTO `article`(`title`,`content`,`create_time`,`user_id`) VALUES (:title,:content,:create_time,:user_id)';
        $stmt = $this->_db->prepare($sql);
        $create_time = time();
        $stmt->bindParam(':title',$title);
        $stmt->bindParam(':content',$content);
        $stmt->bindParam(':create_time',$create_time);
        $stmt->bindParam(':user_id',$user_id);
        if(!$stmt->execute()){
            throw new Exception('新建文章失败',ErrorCode::ACTICLE_CREATE_FAIL);
        }
        return[
            'article_id'=>$this->_db->lastInsertId(),
            'title' => $title,
            'content' => $content,
            'create_time' => $create_time,
            'user_id' => $user_id
        ];

    }

    /**
     * 修改文章
     * @param $title
     * @param $content
     * @param $user_id
     * @param $article_id
     * @throws Exception
     * @return array|mixed
     */
    public function edit($title,$content,$user_id,$article_id){
        $article = $this->view($article_id);
        if($article['user_id']!=$user_id){
            throw new Exception('您没有权限修改此文章',ErrorCode::ACTICLE_CONTENT_PERMIT);
        }
        $title = empty($title)?$article['title']:$title;
        $content = empty($content)?$article['content']:$content;
        if($title===$article['title']&&$content===$article['content']){
            return $article;
        }
        $sql = 'UPDATE `article` SET `title`=:title,`content`=:content WHERE `user_id`=:user_id AND `article_id`=:article_id';
        $stmt = $this->_db->prepare($sql);
        $stmt->bindParam(':user_id',$user_id);
        $stmt->bindParam(':article_id',$article_id);
        $stmt->bindParam(':title',$title);
        $stmt->bindParam(':content',$content);
        $stmt->execute();
        $article = $stmt->fetch(PDO::FETCH_ASSOC);
        if(!$article){
            throw new Exception('修改文章失败',ErrorCode::ACTICLE_CONTENT_EDIT_FAIL);
        }
        return[
            'user_id'=>$user_id,
            'article_id'=>$article_id,
            'title'=>$title,
            'content'=>$content
        ];
    }

    /**
     * 显示文章内容
     * @param $article_id
     * @throws Exception
     * @internal param $user_id
     * @return mixed
     */
    public function view($article_id){
        $sql = 'SELECT * FROM `article` WHERE `article_id`=:article_id';
        $stmt = $this->_db->prepare($sql);
        $stmt->bindParam(':article_id',$article_id);
        $stmt->execute();
        $article = $stmt->fetch(PDO::FETCH_ASSOC);
        if(!$article){
            throw new Exception('文章不存在',ErrorCode::ACTICLE_EMPTY);
        }
        return $article;
    }

    /**
     * 删除文章
     * @param $user_id
     * @param $article_id
     * @return int
     * @throws Exception
     */
    public function delete($user_id,$article_id){
        $article = $this->view($article_id);
        if($article['user_id']!==$user_id){
            throw new Exception('您没有权限删除此文章',ErrorCode::ACTICLE_DELETE_PERMIT);
        }
        $sql = 'DELETE FROM `article` WHERE `user_id`=:user_id AND `article_id`=:article_id';
        $stmt = $this->_db->prepare($sql);
        $stmt->bindParam(':user_id',$user_id);
        $stmt->bindParam(':article_id',$article_id);
        if(false===$stmt->execute()){
            throw new Exception('删除文章失败',ErrorCode::ACTICLE_DELETE_FAIL);
        }
        return 1;
    }

    /**
     * 获取文章列表
     * @param $user_id
     * @param int $page
     * @param int $size
     * @throws Exception
     * @return
     */
    public function lists($user_id,$page=1,$size=10){
        if($size>100){
            throw new Exception('分页数不能大于100',ErrorCode::LIST_TO_BIG);
        }
        $sql = 'SELECT * FROM `article` WHERE `user_id`=:user_id LIMIT :limit,:offset';
        $limit = ($page-1)*$size;
        $limit = $limit<0?0:$limit;
        $tmts = $this->_db->prepare($sql);
        $tmts->bindParam(':user_id',$user_id);
        $tmts->bindParam(':limit',$limit);
        $tmts->bindParam(':offset',$size);
        $tmts->execute();
        $list = $tmts->fetchAll(PDO::FETCH_ASSOC);
        if(!$list){
          throw new Exception('获取列表失败',ErrorCode::LIST_FAIL);
        }
        return $list;
}
}